package demo.adapter;

import java.util.ArrayList;

import util.MLog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import demo.bean.PhotoAlbumBean;
import demo.zandroid.R;

/**
 * Created by hechao on 9/17/13.
 */
public class PhotoAlbumAdapter extends ArrayAdapter<PhotoAlbumBean> {
    private ArrayList<PhotoAlbumBean> albumBeans;
    private Context context;
    //private SimpleDateFormat dateformat;
    private ImageLoader imageLoader;
    private DisplayImageOptions optionsIcon;

    public PhotoAlbumAdapter(Context context, ArrayList<PhotoAlbumBean> beans) {
        super(context, R.layout.activity_photo_album_item, beans);
        this.context = context;
        this.albumBeans = beans;
        //this.dateformat = new SimpleDateFormat("MM-dd HH:mm");

        imageLoader = ImageLoader.getInstance();
        optionsIcon = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).showStubImage(R.drawable.head135)
                .showImageForEmptyUri(R.drawable.head135).showImageOnFail(R.drawable.head135)
                .bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(0)).build();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HodlerView hodler;
        if (convertView == null) {
            convertView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_photo_album_item, parent, false);
            hodler = new HodlerView();
            hodler.imgPhoto = (ImageView)convertView.findViewById(R.id.thumbnail);
            hodler.textContent = (TextView)convertView.findViewById(R.id.textName);
            convertView.setTag(hodler);
        }else{
            hodler = (HodlerView)convertView.getTag();
        }

        PhotoAlbumBean bean = albumBeans.get(position);
MLog.i("PhotoAlbumAdapter,getView()", ""+bean);
        imageLoader.displayImage(bean.arrayImages.get(bean.arrayImages.size()-1).uri,
        		hodler.imgPhoto, optionsIcon);
//hodler.imgPhoto.setImageURI(Uri.parse(bean.arrayImages.get(bean.arrayImages.size()-1).uri));
        
		hodler.textContent.setText(bean.strAlbumName+" ("+bean.photoNum+")");
        return convertView;
    }

    private class HodlerView {
        ImageView imgPhoto;
        TextView textContent;
    }
}
